package com.realgodo.site.entity.dic;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.springside.modules.orm.hibernate.DefaultEntityManager;

import com.realgodo.site.entity.IdEntity;
import com.realgodo.site.entity.space.Space;
import com.realgodo.site.service.dic.ConstellationManager;

@Entity(name = "Constellation")
@Table(name = "constellation")
@Inheritance(strategy = InheritanceType.JOINED)
public class Constellation extends IdEntity {

	/** */
	private static final long serialVersionUID = 4752318246089015477L;
	private static final String BLANK = "0";
	public static final int DEFAULT_YEAR = 1970;

	/** 开始日期 默认1970年 */
	private Date startDate;
	/** 结束日期 默认1970年 */
	private Date endDate;

	private String name;
	private String en_name;
	/** 简介 */
	private String comment;

	/** 开始月 为开始月+1 */
	private int startMonth = 1;
	/** 结束月 为开始月+1 */
	private int endMonth = 2;

	private int startDay = 20;

	private int endDay = 19;
	/** 同一星座下的关系space */
	private List<Space> relationList;
	/** 是否是当前星座 */
	private int isActive = 0;

	@Transient
	public int getIsActive() {
		if (ConstellationManager.isConstellation(new Date(), this)) {
			isActive = 1;
		} else {
			isActive = 0;
		}
		return isActive;
	}

	public void addRelation(Space space) {
		if (relationList == null) {
			relationList = new ArrayList<Space>();
		}
		relationList.add(space);
	}

	@Transient
	public List<Space> getRelationList() {
		if (relationList == null) {
			relationList = new ArrayList<Space>();
		}
		return relationList;
	}

	public void setRelationList(List<Space> relationList) {
		this.relationList = relationList;
	}

	@Transient
	public int getStartMonth() {
		if (this.startDate != null) {
			Calendar c = Calendar.getInstance();
			c.setTime(startDate);
			return c.get(Calendar.MONTH) + 1;
		}
		return startMonth;
	}

	public void setStartMonth(int startMonth) {
		this.startMonth = startMonth;
	}

	@Transient
	public int getEndMonth() {
		int startMonth = getStartMonth();
		if (startMonth < 12) {
			return startMonth + 1;
		} else {
			return 1;
		}
	}

	public void setEndMonth(int endMonth) {
		this.endMonth = endMonth;
	}

	@Transient
	public int getStartDay() {
		if (this.startDate != null) {
			Calendar c = Calendar.getInstance();
			c.setTime(startDate);
			return c.get(Calendar.DAY_OF_MONTH);
		}
		return startDay;
	}

	public void setStartDay(int startDay) {
		this.startDay = startDay;
	}

	@Transient
	public int getEndDay() {
		if (this.endDate != null) {
			Calendar c = Calendar.getInstance();
			c.setTime(endDate);
			return c.get(Calendar.DAY_OF_MONTH);
		}
		return endDay;
	}

	public void setEndDay(int endDay) {
		this.endDay = endDay;
	}

	@Transient
	public Date getCompareEndDate() {
		Calendar c = Calendar.getInstance();
		c.setTime(this.getEndDate());
		if (c.get(Calendar.MONTH) == 0) {
			c.set(Calendar.YEAR, c.get(Calendar.YEAR) + 1);
			return c.getTime();
		}
		return endDate;
	}

	@Temporal(TemporalType.DATE)
	@Column(name = "start_date", insertable = true, updatable = true, nullable = false)
	public Date getStartDate() {
		if (startDate == null) {
			String date_str = String.valueOf(DEFAULT_YEAR)
					+ (startMonth < 10 ? BLANK + startMonth : startMonth)
					+ (startDay < 10 ? BLANK + startDay : startDay);
			try {
				startDate = DefaultEntityManager.dateSdf.parse(date_str);
			} catch (ParseException e) {
				startDate = new Date();
			}
		}
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	@Temporal(TemporalType.DATE)
	@Column(name = "end_date", insertable = true, updatable = true, nullable = false)
	public Date getEndDate() {
		if (endDate == null) {
			String date_str = String.valueOf(DEFAULT_YEAR)
					+ (endMonth < 10 ? BLANK + endMonth : endMonth)
					+ (endDay < 10 ? BLANK + endDay : endDay);
			try {
				endDate = DefaultEntityManager.dateSdf.parse(date_str);
			} catch (ParseException e) {
				endDate = new Date();
			}
		}
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Lob
	@Basic(fetch = FetchType.LAZY)
	@Column(name = "comment", insertable = true, updatable = true)
	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	// public String getKeyWord() {
	// return keyWord;
	// }
	// public void setKeyWord(String keyWord) {
	// this.keyWord = keyWord;
	// }
	// public String getLuckNumber() {
	// return luckNumber;
	// }
	// public void setLuckNumber(String luckNumber) {
	// this.luckNumber = luckNumber;
	// }
	// public String getCharacteristic() {
	// return characteristic;
	// }
	// public void setCharacteristic(String characteristic) {
	// this.characteristic = characteristic;
	// }
	// public String getWords() {
	// return words;
	// }
	// public void setWords(String words) {
	// this.words = words;
	// }
	// public String getDisposition() {
	// return disposition;
	// }
	// public void setDisposition(String disposition) {
	// this.disposition = disposition;
	// }
	// public String getHappyEvent() {
	// return happyEvent;
	// }
	// public void setHappyEvent(String happyEvent) {
	// this.happyEvent = happyEvent;
	// }
	// public String getPainEvent() {
	// return painEvent;
	// }
	// public void setPainEvent(String painEvent) {
	// this.painEvent = painEvent;
	// }
	// public String getFateConstellation() {
	// return fateConstellation;
	// }
	// public void setFateConstellation(String fateConstellation) {
	// this.fateConstellation = fateConstellation;
	// }
	// public String getNoFateConstellation() {
	// return noFateConstellation;
	// }
	// public void setNoFateConstellation(String noFateConstellation) {
	// this.noFateConstellation = noFateConstellation;
	// }
	// public String getIndividuality() {
	// return individuality;
	// }
	// public void setIndividuality(String individuality) {
	// this.individuality = individuality;
	// }

	public String getEn_name() {
		return en_name;
	}

	public void setEn_name(String en_name) {
		this.en_name = en_name;
	}

}
